<?php

namespace app\model;

use app\utils\Md5Service;
use \think\facade\Db;
use think\facade\Log;

class PartnerUserModel
{

    /**
     * 获取所有的权限菜单列表
     * @return void
     */
    public static function GetAllPermission()
    {
        return Db::table('xl_partner_permission')->where('status', '=', 1)
            ->select();

    }

    /**
     * 根据手机号密码获取登录信息是否正确
     * @return void
     */
    public static function GetPartneInfo($param)
    {
        return Db::table('xl_partner_user')->where('mobile', $param['mobile'])
            ->where('password', Md5Service::encryptMd5($param['password'], $param['salt']))
            ->find();

    }

    public static function UpdatePartnerLoginInfo1($data, $param)
    {
        $update['login_ip'] = $param['login_ip'];
        $update['login_num'] = $data['login_num'] + 1;
        $update['login_time'] = getNowDateTime();
        Db::startTrans();
        try {
            Db::table('xl_partner_user')->where(['id' => $data['id']])->update($update);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {

            // 回滚事务
            Db::rollback();
            return false;
        }
    }

    public static function getRoleByUid($uid = 0)
    {
        return Db::table('xl_partner_user')->where(['id' => $uid])->field('id,role_id')->find();

    }

    public static function getPartnerIdByUid($uid = 0)
    {
        return Db::table('xl_partner_user')->where(['id' => $uid])->field('id,role_id,partner_id,type')->find();
    }

    public static function getCompanyIdByUid($uid = 0)
    {
        return Db::table('xl_partner_user')->where(['id' => $uid])->field('id,role_id,company_id,type')->find();
    }
    
    public static function getAllPartnerInfoById($id)
    {

        return Db::table('xl_partner_user')->alias('user')
            ->where('user.partner_id', '=', $id)
            ->field('user.id,user.name,user.mobile,user.avatar,user.status,user.login_time,user.nickname,user.role_id')
            ->find();
    }

    public static function getAllPartnerInfoByUId($uid)
    {

        return Db::table('xl_partner_user')->alias('user')
            ->where('user.id', '=', $uid)
            ->field('user.id,user.name,user.mobile,user.avatar,user.status,user.login_time,user.nickname,user.role_id')
            ->find();
    }

    //根据partner_id获取所有的user
    public static function getPartnerUserByPartnerId($param)
    {
        return Db::table('xl_partner_user')->alias('user')
            ->join('xl_partner_role role', 'user.role_id = role.id', 'left')
            ->where(['user.partner_id' => $param['partner_id']])
            ->where('user.status', "<", 3)
            ->field('user.id,user.name,user.mobile,user.status,user.login_time,user.nickname,user.role_id,role.name as role_name,user.warehouse_ids')
            ->select()->toArray();
    }

    //添加合作商新用户

    public static function add_user($data)
    {
        return Db::table('xl_partner_user')->insertGetId($data);
    }

    //修改合作商新用户

    public static function modify_user($data, $id)
    {
        return Db::table('xl_partner_user')->where("id", "=", $id)->update($data);
    }

    //删除合作商新用户

    public static function del_user($id)
    {

        return Db::table('xl_partner_user')->where("id", "=", $id)->update(['status' => 3]);
    }

    //根据手机号查询合作商用户

    public static function find_user($mobile)
    {
        return Db::table('xl_partner_user')->where("mobile", '=', trim($mobile))
//            ->where("id","<>",$id)
            ->find();
    }

    public static function find_user2($mobile, $id)
    {
        return Db::table('xl_partner_user')->where("mobile", '=', trim($mobile))
            ->where("id", "<>", $id)
            ->find();
    }

    //根据手机号查询合作商用户

    public static function findUserById($id)
    {
//        $key='partner_user_'.$id;
//        $redisLink=linkRedis(6);
//        $partnerUserInfo=$redisLink->get($key);
//        if ($partnerUserInfo){
//            return json_decode($partnerUserInfo,true);
//        }else{
//
//        }

        $data = Db::table('xl_partner_user')->where("id", '=', $id)->find();
        return $data;
//        $redisLink->set($key,json_encode($data));
    }

    /**
     * 根据手机号密码获取登录信息是否正确
     * @return void
     */
    public static function GetPartnerUserInfo($param)
    {
        return Db::table('xl_partner_user')->where('mobile', $param['mobile'])
            ->find();

    }


    public static function UpdatePartnerUserLoginInfo($data, $param, $newApiSalt, $login_channel)
    {
        $update['login_ip'] = $param['login_ip'];
        if ($login_channel == 1) {
            $update['api_salt'] = $newApiSalt;

        } elseif ($login_channel == 2) {
            $update['wms_api_salt_h5'] = $newApiSalt;
        }
        $update['login_num'] = $data['login_num'] + 1;
        $update['login_time'] = getNowDateTime();
        Db::startTrans();
        try {
            Db::table('xl_partner_user')->where(['id' => $data['id']])->update($update);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {

            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    //新建仓库管理员
    public static function addPartnerUserInfo($data, $logParam, $id)
    {

        Db::startTrans();
        try {
            Db::table('xl_partner_user')->insert($data);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            echo $e->getMessage();

            // 回滚事务
            Db::rollback();
            return false;
        }
    }

    //新建仓库管理员
    public static function modifyPartnerUserInfo($data, $logParam, $id): bool
    {

        Db::startTrans();
        try {
            Db::table('xl_partner_user')->where("id", "=", $id)->save($data);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            echo $e->getMessage();

            // 回滚事务
            Db::rollback();
            return false;
        }
    }

    //删除仓库管理员
    public static function delPartnerUserInfo($data, $logParam, $id): bool
    {

        Db::startTrans();
        try {
            Db::table('xl_partner_user')->where("id", "=", $id)->save(['status' => 3]);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            echo $e->getMessage();

            // 回滚事务
            Db::rollback();
            return false;
        }
    }

    /**
     * 根据手机号密码获取登录信息是否正确
     * @return void
     */
    public static function admin_lists($param)
    {
        $where[] = ['warehouse_id', '=', $param['warehouse_id']];
        $where[] = ['partner_id', '=', $param['partner_id']];
        $where[] = ['status', '<', 3];

        return Db::table('xl_partner_user')->where($where)
            ->field('id,name,mobile,salt')
            ->select();

    }


}
